package springboot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springboot.bean.AgvTaskReturn;
import springboot.kernel.KernelTools;
import springboot.service.AgvService;

import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.Date;

@Slf4j
@Component
@WebService(serviceName = "AgvService", // 与接口中指定的name一致
        targetNamespace = "http://service.springboot/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "springboot.service.AgvService"// 接口地址
)
public class AgvServiceImpl implements AgvService {

    @Autowired
    private KernelTools opentcsClient;

    @Override
    public AgvTaskReturn sendAgvTask(@WebParam(name = "taskNo")String taskNo,
                           @WebParam(name = "source")String source,
                           @WebParam(name = "destination")String destination,
                           @WebParam(name = "status")String status,
                           @WebParam(name = "agvId")String agvId,
                           @WebParam(name = "creatData")Date creatData,
                           @WebParam(name = "isClosed")String isClosed){
        AgvTaskReturn agvTaskReturn=null;
        try {
            log.info("agv task response start");
            if (status != null) {
                if (status.equals("0")) {
                    if (taskNo != null && source != null && destination != null) {
                        log.info("factor is ok , login to sendAgvTask");
                        opentcsClient.CreateOrderOneStation(source, destination, taskNo);
                        agvTaskReturn=new AgvTaskReturn("success","task is created");
                        log.info("agv task response end");
                        return agvTaskReturn;
                    }
                    agvTaskReturn=new AgvTaskReturn("failed","taskNo|source|destination is null");
                    log.info("agv task response end");
                    return agvTaskReturn;
                }
                agvTaskReturn=new AgvTaskReturn("failed","status is not equal 0");
                log.info("agv task response end");
                return agvTaskReturn;
            }
            agvTaskReturn=new AgvTaskReturn("failed","status is null");

        }catch (Exception e){
            log.info("error is at agv task {}",e);
            e.printStackTrace();
        }
        log.info("agv task response end");
        return agvTaskReturn;
    }

    @Override
    public String returnAgvStatus(String factoryId){
        try {
            log.info("agv status response start");
            String V1 = "Vehicle-0001";
            if("7011_2".equals(factoryId)){
                String state = opentcsClient.getVehicleStatus(V1);//获得车的状态	   //state,procstate来判断状态
                String procstate = opentcsClient.getProcState(V1);//获得当前车执行的状态(通过State与Processing state来判断)
                String level = opentcsClient.getinter(V1);//获得该车的调用级别
                String orderName=opentcsClient.getOrderName(V1);
                if((state.equals("IDLE") && procstate.equals("IDLE") && level.equals("TO_BE_UTILIZED"))
                        || (state.equals("EXECUTING") && orderName.startsWith("Recharge") && level.equals("TO_BE_UTILIZED"))
                        || (state.equals("EXECUTING") && orderName.startsWith("Park") && level.equals("TO_BE_UTILIZED"))
                        || (state.equals("CHARGING") && level.equals("TO_BE_UTILIZED"))) {
                    log.info("IDEA");
                    log.info("agv status response end");
                    return "0001";
                } else if (state.equals("EXECUTING") && orderName.startsWith("TO")) {
                    log.info("EXECUTING");
                    log.info("agv status response end");
                    return "0";
                } else if (state.equals("ERROR")) {
                    log.info("ERROR");
                    log.info("agv status response end");
                    return "0";
                } else if (state.equals("UNKNOWN")) {
                    log.info("UNKNOWN");
                    log.info("agv status response end");
                    return "0";
                }else {
                    log.info("agv status response end");
                    return "0";
                }
            }else {
                log.info("agv status response end");
                return "0";
            }
        }catch (Exception e){
            log.info("error is at agv status {}",e);
            e.printStackTrace();
        }
        log.info("agv status response end");
        return "0";
    }
}
